package com.dwx.guli.service.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dwx.guli.service.base.result.R;
import com.dwx.guli.service.edu.entity.Course;
import com.dwx.guli.service.edu.entity.Teacher;
import com.dwx.guli.service.edu.entity.query.TeacherQuery;
import com.dwx.guli.service.edu.feign.OssFileService;
import com.dwx.guli.service.edu.mapper.CourseMapper;
import com.dwx.guli.service.edu.mapper.TeacherMapper;
import com.dwx.guli.service.edu.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author dwx
 * @since 2021-06-30
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {
    @Autowired
    private OssFileService ossFileService;
    @Autowired
    private CourseMapper courseMapper;
    @Override
    public IPage<Teacher> selectPage(Long page, Long limit, TeacherQuery teacherQuery) {
        Page<Teacher> pageModel=new Page<>(page,limit);
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");

        if(null==teacherQuery){
            baseMapper.selectPage(pageModel,queryWrapper);
            return pageModel;
        }
        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String joinDateBegin = teacherQuery.getJoinDateBegin();
        String joinDateEnd = teacherQuery.getJoinDateEnd();

        queryWrapper.likeRight(!StringUtils.isEmpty(name),"name",name);
        queryWrapper.eq(null!=level,"level",level);
        queryWrapper.ge(!StringUtils.isEmpty(joinDateBegin),"join_date",joinDateBegin);
        queryWrapper.le(!StringUtils.isEmpty(joinDateEnd),"join_date",joinDateEnd);
        baseMapper.selectPage(pageModel,queryWrapper);
        return pageModel;
    }

    @Override
    public List<Map<String, Object>> selectNameListByKey(String key) {
        QueryWrapper<Teacher > queryWrapper=new QueryWrapper<>();
        queryWrapper.select("name");
        queryWrapper.likeRight(!StringUtils.isEmpty(key),"name",key);
        List<Map<String, Object>> maps = baseMapper.selectMaps(queryWrapper);
        return maps;
    }

    @Override
    public boolean removeAvatarById(String id) {
        Teacher teacher = baseMapper.selectById(id);
        if(null!=teacher){
            String avatar = teacher.getAvatar();
            if(!StringUtils.isEmpty(avatar)){
                R r = ossFileService.removeFile(avatar);
                return r.getSuccess();
            }
        }
        return false;
    }

    @Override
    public List<Map<String, Object>> nameList() {
        List<Map<String ,Object >> list=new ArrayList<>();
        QueryWrapper<Teacher> queryWrapper=new QueryWrapper<>();
        queryWrapper.select("id","sort","name");
        List<Teacher> teachers = baseMapper.selectList(queryWrapper);
        for (Teacher teacher : teachers) {
            Map<String ,Object > map=new HashMap<>();
            map.put("id",teacher.getId());
            map.put("name",teacher.getName());
            map.put("sort",teacher.getSort());
            list.add(map);
        }
        return list;
    }

    @Override
    public Map<String, Object> selectTeacherInfoById(String id) {
        Teacher teacher = baseMapper.selectById(id);
        QueryWrapper<Course> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("teacher_id",id);
        List<Course> courses = courseMapper.selectList(queryWrapper);
        Map<String ,Object> map=new HashMap<>();
        map.put("teacher",teacher);
        map.put("courseList",courses);
        return map;
    }
}
